package com.kevin.pay.exception;

import com.kevin.pay.domain.JsonData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData handler(Exception e){
        if(e instanceof CustomException){
            CustomException customException= (CustomException) e;
            return JsonData.buildError(customException.getMsg(),customException.getCode());
        }else{
            return JsonData.buildError("全局异常，未知错误:"+e.getMessage());
        }
    }
}
